﻿#include "SectionInfoDialog.h"
#include "OperatorsInterface/ParaWidgetInterfaceOperator.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionBeam.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionSolidHomogeneous.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "GUIFrame/MainWindow.h"
#include "ui_SectionInfoDialog.h"
#include <QMessageBox>
#include <QDebug>

#include "FITK_Interface/FITKInterfaceFlowOF/FITKOFPhysicsData.h"

namespace GUI
{
    SectionInfoDialog::SectionInfoDialog(QWidget* mw, EventOper::ParaWidgetInterfaceOperator* oper, QString name, Interface::FITKAbaAbastractSection::FITKAbaSectionType type) :
        GUIDialogBase(mw), _ui(new Ui::SectionInfoDialog), _oper(oper), _name(name), _type(type), _isCreate(true)
    {
        _ui->setupUi(this);
        initDialog();
    }

    SectionInfoDialog::SectionInfoDialog(QWidget* mw, EventOper::ParaWidgetInterfaceOperator* oper, Interface::FITKAbaAbastractSection* section) :
        GUIDialogBase(mw), _ui(new Ui::SectionInfoDialog), _oper(oper), _section(section), _isCreate(false)
    {
        _ui->setupUi(this);
        if (_section == nullptr) return;
        _name = _section->getDataObjectName();
        _type = _section->getSectionType();
        initDialog();
    }

    SectionInfoDialog::~SectionInfoDialog()
    {
        delete _ui;
    }

    void SectionInfoDialog::initDialog()
    {
        // 初始化界面
        _ui->lb_nametext->setText(_name);
        switch (_type)
        {
            //梁截面
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::Beam:
            _ui->w_thickness->setVisible(false);
            _ui->w_integration->setVisible(false);
            _ui->lb_typetext->setText("Beam,Constant");
            break;
            //壳单元
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::ShellHomogeneous:
            _ui->w_pname->setVisible(false);
            _ui->w_pshape->setVisible(false);
            _ui->lb_typetext->setText("Shell,Homogeneous");
            break;
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::SolidHomogeneous:
            _ui->w_thickness->setVisible(false);
            _ui->w_integration->setVisible(false);
            _ui->w_pname->setVisible(false);
            _ui->w_pshape->setVisible(false);
            _ui->lb_typetext->setText("Solid,Homogeneous");
            break;
        }

        // 填充材料下拉列表
        initMaterialList();

        // 壳单元界面
        if (_section != nullptr && _type == Interface::FITKAbaAbastractSection::FITKAbaSectionType::ShellHomogeneous)
        {
//            Interface::FITKAbaSectionShellHomogeneous* section = dynamic_cast<Interface::FITKAbaSectionShellHomogeneous*>(_section);
//            if (section == nullptr) return;
//            _ui->le_thickness->setText(QString("%1").arg(section->getThickness()));
//            _ui->sb_integration->setValue(section->getThicknessIntergrationPoints());
        }
        // 梁单元界面
        else if (_type == Interface::FITKAbaAbastractSection::FITKAbaSectionType::Beam){
            initProfileList();
//            Interface::FITKAbaSectionBeam* section = dynamic_cast<Interface::FITKAbaSectionBeam*>(_section);
//            if (section == nullptr)return;
//            Interface::FITKAbaProfile* proFile = section->getProfile();
//            //comb_pname文本修改
//            comb_pnameTextChange(proFile);
        }
    }

    void SectionInfoDialog::initMaterialList()
    {
        //获取材料管理器
        Interface::FITKMaterialManager* matMananger = FITKAPP->getGlobalData()->getMaterialData<Interface::FITKMaterialManager>();
        _ui->comb_material->clear();
        int count = matMananger->getDataCount();
        int selectedIndex = 0;
        //获取所有材料并填充
        for (int i = 0; i < count; ++i) {
            Interface::FITKAbstractMaterial* mat = matMananger->getDataByIndex(i);
            if (mat == nullptr) continue;
            if (_section != nullptr && _section->getMaterialName() == mat->getDataObjectName()) {
                selectedIndex = _ui->comb_material->count();
            }
            _ui->comb_material->addItem(mat->getDataObjectName(), mat->getDataObjectID());
        }
        //设置当前选择的材料
        _ui->comb_material->setCurrentIndex(selectedIndex);
    }

    void SectionInfoDialog::initProfileList()
    {
        //当前算例获取
//        auto currentCase = AbaqusData::FITKAbaqusData::GetDataFromAppFrame()->getCurrentCase();
//        if (currentCase == nullptr)return;

//        //梁截面管理器获取
//        Interface::FITKAbaProfileManager* beamProfileManager = currentCase->getProfileManager();
//        if (beamProfileManager == nullptr)return;

//        int count = beamProfileManager->getDataCount();

//        Interface::FITKAbaSectionBeam* section = dynamic_cast<Interface::FITKAbaSectionBeam*>(_section);
//        _ui->comb_pname->clear();
//        int selectedIndex = 0;
//        //获取所有的粱截面并填充
//        for (int i = 0; i < count; ++i)
//        {
//            Interface::FITKAbaProfile* beamProfile = beamProfileManager->getDataByIndex(i);
//            if (beamProfile == nullptr) continue;
//            if (section != nullptr && section->getProfile() == beamProfile)
//            {
//                selectedIndex = _ui->comb_pname->count();
//            }
//            _ui->comb_pname->addItem(beamProfile->getDataObjectName(), beamProfile->getDataObjectID());
//        }
//        //设置当前梁截面
//        _ui->comb_pname->setCurrentIndex(selectedIndex);
    }

    bool SectionInfoDialog::checkDialog()
    {
        if (_ui->comb_material->currentIndex() < 0)
        {
            QMessageBox::critical(nullptr, tr("Error Information"), tr("Missing values for material!"));
            return false;
        }
        switch (_type)
        {
            //壳单元
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::ShellHomogeneous:
        {
            QString thickness = _ui->le_thickness->text().trimmed();
            if (thickness.isEmpty())
            {
                QMessageBox::critical(nullptr, tr("Error Information"), tr("A shell thickness value must be specified!"));
                return false;
            }
            bool ok{};
            thickness.toDouble(&ok);
            if (!ok)
            {
                QMessageBox::critical(nullptr, tr("Error Information"), QString(tr("The syntax error in the expression below must be corrected.\n%1")).arg(thickness));
                return false;
            }
            return true;
        }
        //梁
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::Beam:
        {
            if (_ui->comb_pname->currentIndex() < 0)
            {
                QMessageBox::critical(nullptr, tr("Error Information"), tr("Missing values for profile!"));
                return false;
            }
            return true;
        }
        //暂不处理
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::SolidHomogeneous:
        {
            return true;
        }
        }
    }

    void SectionInfoDialog::comb_pnameTextChange(Interface::FITKAbaProfile* profile)
    {
//        if (profile == nullptr) return;
//        //获取截面形状
//        Interface::FITKAbaProfile::FITKAbaProfileShape beamType = profile->getShape();
//        QString beamTypeText = "";
//        //设置截面形状
//        switch (beamType) {
//        case Interface::FITKAbaProfile::FITKAbaProfileShape::Box:beamTypeText = "Box"; break;
//        case Interface::FITKAbaProfile::FITKAbaProfileShape::Pipe:beamTypeText = "Pipe"; break;
//        case Interface::FITKAbaProfile::FITKAbaProfileShape::Circular:beamTypeText = "Circular"; break;
//        case Interface::FITKAbaProfile::FITKAbaProfileShape::Rectangular:beamTypeText = "Rectangular"; break;
//        case Interface::FITKAbaProfile::FITKAbaProfileShape::I:beamTypeText = "I"; break;
//        case Interface::FITKAbaProfile::FITKAbaProfileShape::L:beamTypeText = "L"; break;
//        }
//        _ui->lb_pshapetext->setText(beamTypeText);
    }

    void SectionInfoDialog::on_pb_ok_clicked()
    {
        //获取截面管理器
        //获取界面管理器
        Interface::FITKOFPhysicsData* physicsData = FITKAPP->getGlobalData()->getPhysicsData<Interface::FITKOFPhysicsData>();
        if (physicsData == nullptr)return;
        Interface::FITKAbstractSectionManager* sectionManager =physicsData->getSectionManager();
        if (sectionManager == nullptr)return;

//        //梁截面管理器
//        Interface::FITKAbaProfileManager* beamProfileManager = currentCase->getProfileManager( );
//        if (beamProfileManager == nullptr)return;

        if (!checkDialog()) return;

        switch (_type)
        {
            //梁截面
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::Beam:
        {
//            Interface::FITKAbaSectionBeam* section =
//                _isCreate ? new Interface::FITKAbaSectionBeam : dynamic_cast<Interface::FITKAbaSectionBeam*>(_section);
//            if (section == nullptr) return;
//            section->setProfile(beamProfileManager->getDataByID(_ui->comb_pname->currentData().toInt()));
//            section->setMaterial(_ui->comb_material->currentData().toInt());
//            if (_isCreate) _section = section;
//            break;
        }
        //壳单元
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::ShellHomogeneous:
        {
//            Interface::FITKAbaSectionShellHomogeneous* section =
//                _isCreate ? new Interface::FITKAbaSectionShellHomogeneous : dynamic_cast<Interface::FITKAbaSectionShellHomogeneous*>(_section);
//            if (section == nullptr) return;
//            section->setThickness(_ui->le_thickness->text().toDouble());
//            section->setThicknessIntergrationPoints(_ui->sb_integration->value());
//            section->setMaterial(_ui->comb_material->currentData().toInt());
//            if (_isCreate) _section = section;
//            break;
        }
        case Interface::FITKAbaAbastractSection::FITKAbaSectionType::SolidHomogeneous:
        {
            Interface::FITKAbaSectionSolidHomogeneous* section =
                _isCreate ? new Interface::FITKAbaSectionSolidHomogeneous : dynamic_cast<Interface::FITKAbaSectionSolidHomogeneous*>(_section);
            if (section == nullptr) return;
            section->setMaterial(_ui->comb_material->currentData().toInt());
            if (_isCreate) _section = section;
            break;
        }
        }
        //创建模式
        Interface::FITKMaterialManager* matMananger = FITKAPP->getGlobalData()->getMaterialData<Interface::FITKMaterialManager>();
        int MaterialIDTemp = _ui->comb_material->currentData().toInt();
        Interface::FITKAbstractMaterial* mat =matMananger->getDataByID(MaterialIDTemp);
        int MaterialID = mat->getMaterialID();
        _section->SetMaterialIDTemp(MaterialID);
        if (_isCreate && _section != nullptr)
        {
            _section->setDataObjectName(_ui->lb_nametext->text());
            sectionManager->appendDataObj(_section);
        }
        //执行操作器
        if (_oper != nullptr) {
            _oper->execProfession();
        }

        this->accept();
    }

    void SectionInfoDialog::on_pb_cancel_clicked()
    {
        this->reject();
    }

    void SectionInfoDialog::on_actionMaterialCreate_clicked()
    {
        auto sender = this->sender();
        QString name = sender->objectName();

        Core::FITKActionOperator* actionOperator = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>(name);
        if (actionOperator == nullptr) return;
        actionOperator->setEmitter(sender);
        actionOperator->actionTriggered();
        //材料列表刷新在材料创建对话框中调用
    }

    void SectionInfoDialog::on_actionBeamProfileCreate_clicked()
    {
//        auto    sender = this->sender();
//        QString name = sender->objectName();

//        Core::FITKActionOperator* actionOperator = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>(name);
//        if (actionOperator == nullptr) return;
//        actionOperator->setEmitter(sender);

//        actionOperator->actionTriggered();

        initProfileList();
    }

    void SectionInfoDialog::on_comb_pname_currentIndexChanged(int index)
    {
//        Interface::FITKAbaProfileManager* beamProfileManager = AbaqusData::FITKAbaqusData::GetDataFromAppFrame()->getCurrentCase()->getProfileManager();
//        Interface::FITKAbaProfile* profile = beamProfileManager->getDataByID(_ui->comb_pname->currentData().toInt());
//        if (profile == nullptr)return;

//        //更新界面梁截面类型
//        comb_pnameTextChange(profile);
    }
}  // namespace GUI


